<%--
  Created by IntelliJ IDEA.
  User: LT
  Date: 2018-10-25
  Time: 15:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link href="${pageContext.request.contextPath}/static/hplus/css/bootstrap.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/hplus/css/style.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/hplus/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/hplus/css/animate.min.css" rel="stylesheet">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/date/bootstrap-datetimepicker.min.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrapvalidator/bootstrapValidator.min.css">
    <script src="${pageContext.request.contextPath}/static/hplus/js/jquery.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/hplus/js/bootstrap.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/hplus/js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/hplus/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/date/Moment.js"></script>
    <script src="${pageContext.request.contextPath}/static/date/bootstrap-datetimepicker.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/bootstrapvalidator/bootstrapValidator.min.js"></script>
    <!--sweet提示框-->
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/hplus/css/plugins/sweetalert/sweetalert.css" />
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/hplus/js/plugins/sweetalert/sweetalert.min.js"></script>

   <%--<jsp:include page="${pageContext.request.contextPath}/common/common.jsp" />--%>

    <script>
        function searcha() {
            //返回表格的 Options。
            var opts = $("#usertb").bootstrapTable("getOptions") ;
            var limit = opts.pageSize ;
            var offset = (opts.pageNumber-1) * limit;
            var username = $("#username").val() ;
            $.post("${pageContext.request.contextPath}/user/querySearch",{'username':username,'limit':limit,'offset':offset},function (data) {
                // var mydata = eval("("+data+")") ;
                //重新绑定表格数据
                $("#usertb").bootstrapTable('load',data) ;
            })
        }

        $(function () {
            <!--校验框架-->
            $('#userform').bootstrapValidator({
                message: 'This value is not valid',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    username: {
                        message: 'The username is not valid',
                        validators: {
                            notEmpty: {
                                message: '用户名不能为空！'
                            },
                            stringLength: {
                                min: 6,
                                max: 30,
                                message: '必须是6-30个字符'
                            },
                            regexp: {
                                regexp: /^[a-zA-Z0-9_\.]+$/,
                                message: '字母或者数字开头'
                            }
                        }
                    },
                    pwd:{
                        message:'',
                        validators: {
                            notEmpty: {
                                message: '密码不能为空！'
                            }
                        }
                    },
                    confirmpwd:{
                        message:'',
                        validators: {
                            notEmpty: {
                                message: '确认密码不能为空！'
                            },
                            identical:{
                                field: 'pwd',
                                message:'两次密码不一致'
                            }
                        }

                    },
                    phone:{
                        validators: {
                            notEmpty: {
                                message: '手机号码不能为空！'
                            },
                            regexp: {
                                regexp:             /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/,
                                message: '请填写正确的手机号码'
                            }
                        }

                    }

                }
            });
            <!--时间日历-->
            $('#datetimepicker1').datetimepicker({
                format: 'YYYY-MM-DD',
                locale: moment.locale('zh-cn')
            });
            <!--时间日历-->
            $('#datetimepicker2').datetimepicker({
                format: 'YYYY-MM-DD',
                locale: moment.locale('zh-cn')
            });

            $("#usertb").bootstrapTable({
                url:'${pageContext.request.contextPath}/user/query',
                //设置每页多少条数据
                pageSize:2,
                pageList:[2,4,8,16],
                height:380,
                pageNumber:1,
                search:true,
                //启用分页，但是默认情况下，是客户端分页的
                pagination:true,
                //启用服务器端分页
              //  sidePagination:'server',
                //设置 true 将禁止多选。
                singleSelect:false,
               /* queryParams:function(params){
                    var temp = {  //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                        limit: params.limit,  //页面大小
                        offset: params.offset, //页码
                        username:$("#username").val()
                    };
                    return temp;
                },*/
                columns:[
                    {
                       checkbox:true
                    },
                    {
                        field:'id',
                        title:'编号'
                    },
                    {
                        field:'username',
                        title:'姓名'
                    },
                    {
                        field:'state',
                        title:'状态',
                        formatter:function (a,b,c) {
                            if (b.state==0){
                                return "可用" ;
                            }
                            else{
                                return "不可用" ;
                            }
                        }
                    },
                    {
                        field:'workname',
                        title:'工作姓名'
                    },
                    {
                        field:'sex',
                        title:'性别'
                    },
                    {
                        field:'phone',
                        title:'电话'
                    },
                    {
                        field:'borndate',
                        title:'生日'
                    },
                    {
                        field:'mountguarddate',
                        title:'入职日期'
                    },
                    {
                        title:'操作',
                        formatter:function(value,row,index){
                            return "<button class='btn  btn-primary' onclick='tofenpei("+row.id+")'><span class='glyphicon glyphicon-edit'></span>&nbsp;分配角色</button>&nbsp;&nbsp;<button class='btn  btn-primary' onclick='toxiugai(("+index+"))'><span class='glyphicon glyphicon-pencil'></span>&nbsp;修改</button>"
                        }
                    },
                ]
            }) ;
        }) ;
        //打开增加的对话框
        function toadd() {
            $("#addmodal").modal("show") ;
        }
        //增加用户
        function doadd(){
            //
            //启用较验
            $('#userform').data('bootstrapValidator').validate();
            //验证是否通过true/false
            var flag = $('#userform').data('bootstrapValidator').isValid() ;
            if (flag){
                //
                //绑定当前页开始
                var options = $("#usertb").bootstrapTable('getOptions') ;
                //得到当前页
                var pn = options.pageNumber ;
                $("input[name='pn']").val(pn) ;
                //序列化表单
                var formStr=$('#userform').serialize();
                //处理表单控件的中文乱码
                params = decodeURIComponent(formStr,true);
                $.ajax({
                    'type':'post',
                    'url':'/user/save',
                    'data':params,
                    success:function(data){
                        //关闭对话框
                        $("#addmodal").modal('hide') ;
                        //重新加载表格
                        $("#usertb").bootstrapTable('load',data) ;
                        //提示增加成功
                        swal("提示信息", "增加成功!", "success");
                    }
                })
            }


        }
        //打开修改对话框,回传要修改行的数据
        function toxiugai(index) {
                //取消所有选择
               $("#usertb").bootstrapTable("uncheckAll");
                //选中要修改的行
                $("#usertb").bootstrapTable("check", index);

                //得到所选行的数据
                var row = $("#usertb").bootstrapTable("getSelections")[0];
                //绑定主键
                $("input[name='id']").val(row.id);
                //绑定当前页开始
                var options = $("#usertb").bootstrapTable('getOptions') ;
                //得到当前页
                var pn = options.pageNumber ;
                //绑定当前页
                $("input[name='pn']").val(pn) ;
                $("input[name='username']").val(row.username);
                $("input[name='pwd']").val(row.pwd);
                $("input[name='workname']").val(row.workname);
                $("input[name='phone']").val(row.phone);
                $("input[name='nativeplace']").val(row.nativeplace);
                $("input[name='borndate']").val(row.borndate);
                $("input[name='mountguarddate']").val(row.mountguarddate);

               // $("[name='sex'][value='" + row.sex + "']").attr("checked", "checked");
               var sex = row.sex ;

                if (sex =='男'){
                    $("#a").html("<input type=\"radio\" checked=\"checked\" name=\"sex\" value=\"男\" style=\"margin-top: 2px\"> 男") ;
                    $("#b").html("<input type=\"radio\" name=\"sex\" value=\"女\" style=\"margin-top: 2px\"> 女") ;
                }
                else if (sex =='女'){
                    $("#a").html("<input type=\"radio\"  name=\"sex\" value=\"男\" style=\"margin-top: 2px\"> 男") ;
                    $("#b").html("<input type=\"radio\" checked=\"checked\" name=\"sex\" value=\"女\" style=\"margin-top: 2px\"> 女") ;
                }

                //.打开对话框
                $("#toxiugaimodal").modal("show");

        }
        function doxiugai(){
            //.
            var params = $("#userxiugaiform").serialize();
            //处理表单控件的中文乱码
            params = decodeURIComponent(params,true);
            $.ajax({
                'type':'post',
                'url':'/user/update',
                'data':params,
                success:function(data){
                    //关闭对话框
                    $("#toxiugaimodal").modal('hide') ;
                    //友好的提示
                    swal("提示信息", "修改成功!", "success");
                    //重新加载表格
                    $("#usertb").bootstrapTable('load',data) ;

                }
            })
        }
        function tofenpei(id){
            userinfoid = id ;
            userinfoid = id ;
            var a = 0 ;
            var s = "" ;
            // <div class="radio-inline">
            //<input type='radio' value=>
              //  <label>管理员</label>
                //</div>
            //打开一个对话框
            $("#fenpei").modal('show') ;
            //当对话框加载完成后，绑定年有角色
            $("#fenpei").on("shown.bs.modal",function(){
                //
                if (++a ==1){
                    $.post('${pageContext.request.contextPath}/role/query',{"id":id},function(data){
                        for (var i=0;i<data.length;i++){
                            var r = data[i] ;
                            var id = r.id ;
                            var rname = r.rname ;
                            var ischecked = r.ischecked ;
                            if (ischecked) {
                                s = s + "<div class='radio-inline'><input  name='js' checked type='radio' value=" + id + "></input><label>" + rname + "</label></div>";
                            }
                            else
                            {
                                s = s + "<div class='radio-inline'><input  name='js'  type='radio' value=" + id + "></input><label>" + rname + "</label></div>";
                            }
                        }
                       $("#juesebody").html(s) ;
                    }) ;
                }

            }) ;

        }


        var userinfoid ;

        function dofenpei() {
            //1.得到用户的id，角色的id
            var roleid = $("input[name='js']:checked").val() ;
            //2.send ajax
            $.post("${pageContext.request.contextPath}/user/doFenPei",{'userinfoid':userinfoid,'roleid':roleid},function(){
                //关闭对话框,刷新表格
                $("#fenpei").modal('hide') ;
             //   $("#usertb").bootstrapTable('load') ;
            }) ;
        }
        function todel() {
            var ids = "" ;
            //1.得到要删除的行
            //返回所选的行，当没有选择任何行的时候返回一个空数组。
            var rows = $("#usertb").bootstrapTable("getSelections") ;
            if (rows.length==0){
                swal("提示信息", "请选择要删除的数据!", "error");
            }
            else{
                $.each(rows,function (index,row) {
                    ids = ids + row.id + "-" ;
                }) ;

                swal({
                    title: "您确定要删除这条信息吗",
                    text: "删除后将无法恢复，请谨慎操作！",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "删除",
                    closeOnConfirm: false
                }, function () {
                    //得到当前页
                    //绑定当前页开始
                    var options = $("#usertb").bootstrapTable('getOptions') ;
                    //得到当前页
                    var pn = options.pageNumber ;
                    $.post("/user/del",{'ids': ids,'pn':pn},function(data){
                        //友好的提示
                        swal("删除成功！", "您已经永久删除了这条信息。", "success");
                        //刷新表格
                        $("#usertb").bootstrapTable('load',data) ;
                    })

                });

            }
        }

    </script>

</head>
<body>
<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">用户列表</h3>
    </div>
    <div class="panel-body">
        <div class="row">
            <div class="col-sm-6">
                <button onclick="toadd()" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span>&nbsp;&nbsp;增加</button>
                <button onclick="todel()" class="btn btn-primary"><span class="glyphicon glyphicon-trash" "></span>&nbsp;&nbsp;删除</button>
            </div>
            <div class="col-sm-6">
                <form  class="form-inline pull-right">
                    <input id="username" type="text" class="form-control">
                    <button class="btn btn-primary" onclick="searcha()" type="button"><span class="glyphicon glyphicon-search"></span>&nbsp;&nbsp;搜索</button>
                </form>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <table id="usertb"></table>
            </div>
        </div>

    </div>
</div>
<!--增加模态框-->
<div  class="modal inmodal" id="addmodal">
    <div class="modal-dialog">
        <form class="form-horizontal" id="userform"  method="post" role="form">
            <input type="hidden" name="pn">
            <div class="modal-content animated bounceInLeft">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">注册用户</h4>
            </div>
            <div class="modal-body">
                    <div class="form-group">
                        <label class="col-sm-4 text-right" for="username">用户名</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="username"  placeholder="用户名" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="pwd" class="col-sm-4 text-right">密码</label>
                        <div class="col-sm-6">
                         <input type="password" class="form-control" name="pwd"  />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="confirmpwd" class="col-sm-4 text-right">确认密码</label>
                        <div class="col-sm-6">
                        <input type="password" class="form-control" name="confirmpwd"  />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="workname" class="col-sm-4 text-right">workname</label>
                        <div class="col-sm-6">
                        <input type="text" class="form-control" name="workname"  />
                        </div>
                    </div>
                   <div class="form-group">
                        <label for="sex" class="col-sm-4 text-right">性别</label>
                        <div class="col-sm-6">
                            <div class="radio-inline" style="margin-top: -8px">
                                <input type="radio" name="sex" value="男" style="margin-top: 2px">
                              男
                            </div>

                            <div class="radio-inline" style="margin-top: -8px">
                                <input type="radio" name="sex" value="女" style="margin-top: 2px"> 女
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="phone" class="col-sm-4 text-right">手机</label>
                        <div class="col-sm-6">
                             <input type="text" class="form-control" name="phone"  />
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="borndate" class="col-sm-4 text-right">生日：</label>
                        <div class="col-sm-6">
                            <div class="input-group" id="datetimepicker1">
                                <input name="borndate" type='text' class="form-control" />
                                <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                            </div>
                        </div>
                    </div>

                <div class="form-group">
                    <label for="mountguarddate" class="col-sm-4 text-right">入职时间：</label>
                    <div class="col-sm-6">
                        <div class="input-group" id="datetimepicker2">
                            <input name="mountguarddate" type='text' class="form-control" />
                            <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                        </div>
                    </div>
                </div>
                    <div class="form-group">
                        <label for="nativeplace" class="col-sm-4 text-right">籍贯</label>
                        <div class="col-sm-6">
                        <input type="text" class="form-control" name="nativeplace">
                        </div>
                        </div>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                <button onclick="doadd()" type="button" class="btn btn-primary">保存</button>
            </div>
        </div>
        </form>
    </div>
</div>
<!--修改模态框-->
<div  class="modal inmodal" id="toxiugaimodal">
    <div class="modal-dialog">
        <form class="form-horizontal" id="userxiugaiform"  method="post" role="form">

            <input type="hidden" name="id">
            <input type="hidden" name="pn">
            <div class="modal-content animated bounceInLeft">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title">注册用户</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label class="col-sm-4 text-right" for="username">用户名</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="username"  placeholder="用户名" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="pwd" class="col-sm-4 text-right">密码</label>
                        <div class="col-sm-6">
                            <input type="password" class="form-control" name="pwd"  />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="workname" class="col-sm-4 text-right">workname</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="workname"  />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sex" class="col-sm-4 text-right">性别</label>
                        <div class="col-sm-6">
                            <div id="a" class="radio-inline" style="margin-top: -8px">
                                <input type="radio" value="男" name="sex"> 男
                            </div>

                            <div id="b" class="radio-inline" style="margin-top: -8px">
                                <input type="radio" value="女" name="sex"> 女
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="phone" class="col-sm-4 text-right">手机</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="phone"  />
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="borndate" class="col-sm-4 text-right">生日：</label>
                        <div class="col-sm-6">
                            <div class="input-group" id="datetimepicker1">
                                <input name="borndate" type='text' class="form-control" />
                                <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="mountguarddate" class="col-sm-4 text-right">入职时间：</label>
                        <div class="col-sm-6">
                            <div class="input-group" id="datetimepicker2">
                                <input name="mountguarddate" type='text' class="form-control" />
                                <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="nativeplace" class="col-sm-4 text-right">籍贯</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="nativeplace">
                        </div>
                    </div>
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                    <button onclick="doxiugai()" type="button" class="btn btn-primary">修改</button>
                </div>

                <script>
                    function smt(){
                        //启用较验
                        $('#userform').data('bootstrapValidator').validate();
                        //验证是否通过true/false
                        var flag = $('#userform').data('bootstrapValidator').isValid() ;
                        if (flag){
                            //序列化表单
                            var formStr=$('#userform').serialize();
                            //处理表单控件的中文乱码
                            params = decodeURIComponent(formStr,true);
                            $.ajax({
                                'type':'post',
                                'url':'/user/save',
                                'data':params,
                                success:function(data){
                                    //关闭对话框
                                    $("#addmodal").modal('hide') ;
                                    //重新加载表格
                                    $("#usertb").bootstrapTable('load',data) ;
                                }
                            })
                        }


                    }
                </script>
            </div>
        </form>
    </div>
</div>


<!--分配角色的对话框-->
<!-- Modal -->
<div class="modal fade" id="fenpei" >
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" >提示信息</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-sm-12" id="juesebody">

                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button onclick="dofenpei()" type="button" class="btn btn-primary">分配</button>
            </div>
        </div>
    </div>
</div>

</body>
</html>
